﻿using System;
using System.Windows;
using System.Text;

namespace UI.Dialogs
{
    /// <summary>
    /// Interaction logic for ExceptionDialog.xaml
    /// </summary>
    public partial class ExceptionDialog : Window
    {
        Exception _exception;
        public Exception Exception
        {
            get
            {
                return _exception;
            }
            set
            {
                _exception = value;
                GenerateExceptionText(_exception);
                ShowDetails.IsEnabled = _exception != null;
            }
        }

        void GenerateExceptionText(Exception exception)
        {
            StringBuilder text = new StringBuilder();
            text.AppendFormat("Message: {0}\n", exception.Message);
            text.AppendFormat("Stacktrace:\n{0}\n", exception.StackTrace);

            ExceptionText.Text = text.ToString();
        }

        bool _canContinue;
        public bool CanContinue
        {
            get
            {
                return _canContinue;
            }
        }

        public ExceptionDialog()
        {
            InitializeComponent();

            
            ExceptionDetails.Visibility = Visibility.Collapsed;
        }

        private void ToggleButton_Checked(object sender, RoutedEventArgs e)
        {
             if(ShowDetails.IsChecked.GetValueOrDefault())
             {
                 ExceptionDetails.Visibility = Visibility.Visible;
             }
             else
             {
                 ExceptionDetails.Visibility = Visibility.Collapsed;
             }
        }

        private void Continue_Click(object sender, RoutedEventArgs e)
        {
            _canContinue = true;
            Close();
        }

        private void Terminate_Click(object sender, RoutedEventArgs e)
        {
            _canContinue = false;
            Close();
        }

        private void CopyExceptionDetails_Click(object sender, RoutedEventArgs e)
        {
            Clipboard.SetText(ExceptionText.Text);
        }

        private void CopyExceptionMessage_Click(object sender, RoutedEventArgs e)
        {
            Clipboard.SetText(_exception.Message);
        }
    }
}
